from django.db import DatabaseError
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.permissions import PermissionModelSerializer, ContentTypeModelSerializer, \
    GroupModelSerializer, AdminModelSerializer
from django.contrib.auth.models import Permission, Group
from django.contrib.contenttypes.models import ContentType
from users.models import User


class PermissionView(ModelViewSet):
    serializer_class = PermissionModelSerializer
    queryset = Permission.objects.all()
    permission_classes = [IsAdminUser]
    lookup_value_regex = '\d+'

    def content_type(self, request):
        contents = ContentType.objects.all()
        ser = ContentTypeModelSerializer(contents, many=True)
        return Response(ser.data)

    def perm_simple(self, request):
        ser = self.get_serializer(self.get_queryset(), many=True)
        return Response(ser.data)


class GroupView(ModelViewSet):
    serializer_class = GroupModelSerializer
    permission_classes = [IsAdminUser]
    queryset = Group.objects.all()
    lookup_value_regex = '\d+'

    def group_simple(self, request):
        ser = self.get_serializer(self.get_queryset(), many=True)
        return Response(ser.data)


class AdminView(ModelViewSet):
    serializer_class = AdminModelSerializer
    queryset = User.objects.filter(is_superuser=False, is_staff=True)
    permission_classes = [IsAdminUser]
    lookup_value_regex = '\d+'
